# 2.1 创建流程实现
# ① 导⼊进程包
import multiprocessing
import os
import time


def music(n1, music_name):
    for i in range(n1):
        print(f"1子进程{os.getpid()}开始执行任务...")
        print(f'听{music_name}⾳乐...')
        time.sleep(0.2)


def coding(n2, title):
    for i in range(n2):
        print(f"2子进程{os.getpid()}开始执行任务...")
        print(f'敲{title}代码...')
        time.sleep(0.2)


if __name__ == '__main__':
    # ② 通过进程类创建进程对象
    # 进程对象 = multiprocessing.Process()
    print('主进程开始', os.getppid())
    process1 = multiprocessing.Process(target=music, args=(5, '青花瓷'), name='播放音乐进程')  # 创建进程对象
    # process2 = multiprocessing.Process(target=coding, kwargs={'n2': 4, 'title': 'Python'})
    # ③ 启动进程执⾏任务
    # 进程对象.start()
    process1.start()  # 启动进程
    # process2.start()

    # ④ 结束进程
    time.sleep(1)
    process1.join()  # 等待进程结束
    # process2.join()
    print('主进程结束')
